package com.ferryc.modules.sys.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ferryc.commons.utils.CacheUtils;
import com.ferryc.commons.utils.StringUtil;
import com.ferryc.commons.web.BaseController;
import com.ferryc.constant.GlobalConstant;
import com.ferryc.constant.ResultConstant;
import com.ferryc.modules.sys.entity.SysDict;
import com.ferryc.modules.sys.entity.param.SysDictParam;
import com.ferryc.modules.sys.service.ISysDictService;
import com.ferryc.modules.sys.util.DictUtils;

/**
 * 
 * 类名: SysDictController <br/>
 * 作用： 控制<br/>
 * 方法： <br/>
 * 创建者: Ferry C <br/>
 * 添加时间: 2017年11月22日 09:14:51 <br/>
 */
@Controller
@RequestMapping("/dict")
public class SysDictController extends BaseController {

	/** */
	@Autowired
	private ISysDictService sysDictService;

	@RequiresPermissions("dict")
	/** */
	@GetMapping("/")
	public String manage(Model model) {
		return "sys/dict/list";
	}

	/**
	 * 列表查询
	 * 
	 * @param sysDictParam
	 *            请求参数
	 * @return
	 */
	@RequiresPermissions("dict")
	@PostMapping("/list")
	@ResponseBody
	public Object list(SysDictParam sysDictParam) {
		sysDictParam.setStatus(StringUtil.EMPTY);
		SysDict sysDict = sysDictParam.toSysDict();
		sysDict.setParentIdAndId(sysDictParam.getParentId());
		sysDict.setParentId(StringUtil.EMPTY);
		return sysDictService.selectPage(sysDict);
	}

	/**
	 * 字典树查询
	 * 
	 * @param sysDictParam
	 *            请求参数
	 * @return
	 */
	@GetMapping("/treeList")
	@ResponseBody
	public Object treeList(SysDictParam sysDictParam) {
		return sysDictService.selectTreeList(sysDictParam.toSysDict());
	}

	/**
	 * 进入新增页面
	 * 
	 * @param sysLogInfoParam
	 *            请求参数
	 * @param model
	 *            Model
	 * @param request
	 *            request
	 * @return
	 */
	@RequiresPermissions("dict_add")
	@GetMapping("/addPage")
	public Object addPage(SysDictParam sysDictParam, Model model, HttpServletRequest request) {
		model.addAttribute(GlobalConstant.PARENT_ID, request.getParameter(GlobalConstant.PARENT_ID));
		return "sys/dict/add";
	}

	/**
	 * 新增保存
	 * 
	 * @param sysDictParam
	 *            请求参数
	 * @param bindingResult
	 *            数据校验结果
	 * @return
	 */
	@RequiresPermissions("dict_add")
	@PostMapping("/insert")
	@ResponseBody
	public Object insert(@Valid SysDictParam sysDictParam, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			List<FieldError> errorList = bindingResult.getFieldErrors();
			return renderError(errorList);
		}
		boolean flag = sysDictService.checkTypeCode(sysDictParam.toSysDict());
		if (!flag) {
			return renderError(ResultConstant.EXISTS_TYPE_CODE);
		}
		sysDictParam.preInsert();
		sysDictService.insert(sysDictParam.toSysDict());
		CacheUtils.remove(DictUtils.CACHE_DICT_MAP);
		return renderSuccess(ResultConstant.INSERT_SUCCESS);
	}

	/**
	 * 进入查看页面
	 * 
	 * @param sysDictParam
	 *            请求参数
	 * @param model
	 *            Model
	 * @param request
	 * @return
	 */
	@RequiresPermissions("dict_view")
	@GetMapping("/viewPage")
	public Object viewPage(SysDictParam sysDictParam, Model model, HttpServletRequest request) {
		sysDictParam.setStatus(StringUtil.EMPTY);
		SysDict sysDict = sysDictService.getByEntity(sysDictParam.toSysDict());
		model.addAttribute("info", sysDict);
		model.addAttribute(GlobalConstant.READ_ONLY, Boolean.TRUE);
		return "sys/dict/edit";
	}

	/**
	 * 进入修改页面
	 * 
	 * @param sysDictParam
	 *            请求参数
	 * @param model
	 *            Model
	 * @param request
	 * @return
	 */
	@RequiresPermissions("dict_update")
	@GetMapping("/editPage")
	public Object editPage(SysDictParam sysDictParam, Model model, HttpServletRequest request) {
		sysDictParam.setStatus(StringUtil.EMPTY);
		SysDict sysDict = sysDictService.getByEntity(sysDictParam.toSysDict());
		model.addAttribute("info", sysDict);
		model.addAttribute(GlobalConstant.READ_ONLY, Boolean.FALSE);
		return "sys/dict/edit";
	}

	/**
	 * 修改
	 * 
	 * @param sysDictParam
	 *            请求参数
	 * @param bindingResult
	 *            数据校验结果
	 * @return
	 */
	@RequiresPermissions("dict_update")
	@PostMapping("/save")
	@ResponseBody
	public Object save(@Valid SysDictParam sysDictParam, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			List<FieldError> errorList = bindingResult.getFieldErrors();
			return renderError(errorList);
		}
		boolean flag = sysDictService.checkTypeCode(sysDictParam.toSysDict());
		if (!flag) {
			return renderError(ResultConstant.EXISTS_TYPE_CODE);
		}
		sysDictParam.preUpdate();
		sysDictService.update(sysDictParam.toSysDict());
		CacheUtils.remove(DictUtils.CACHE_DICT_MAP);
		return renderSuccess(ResultConstant.UPDATE_SUCCESS);
	}

	/**
	 * 删除
	 * 
	 * @param sysDictParam
	 *            请求参数
	 * @param bindingResult
	 *            数据校验结果
	 * @return
	 */
	@RequiresPermissions("dict_delete")
	@PostMapping("/delete")
	@ResponseBody
	public Object delete(SysDictParam sysDictParam, BindingResult bindingResult) {
		if (SysDict.SYSTEM_ID.equals(sysDictParam.getId())) {
			return renderError(ResultConstant.ERROR_DEFAULT_SYSTEM);
		}
		boolean flag = sysDictService.checkChild(sysDictParam.toSysDict());
		if (flag) {
			return renderError(ResultConstant.EXISTS_DICT_CHILD);
		}
		sysDictParam.preUpdate();
		sysDictParam.setDelFlag(GlobalConstant.YES);
		sysDictService.update(sysDictParam.toSysDict());
		CacheUtils.remove(DictUtils.CACHE_DICT_MAP);
		return renderSuccess(ResultConstant.DELETE_SUCCESS);
	}

	/**
	 * 类型编码校验
	 * 
	 * @param sysDictParam
	 *            请求参数
	 * @return
	 */
	@PostMapping("/checkTypdeCode")
	@ResponseBody
	public Object checkTypdeCode(SysDictParam sysDictParam) {
		boolean flag = sysDictService.checkTypeCode(sysDictParam.toSysDict());
		if (flag) {
			return renderSuccess();
		} else {
			return renderError(ResultConstant.EXISTS_TYPE_CODE);
		}
	}

}